import axios from 'axios'
import store from '@/store'
import JSONBig from 'json-bigint'
const request = axios.create({
  // baseURL: 'http://toutiao-app.itheima.net/',
  baseURL: 'http://localhost:8000/',
  timeout: 3000,
  transformResponse: [
    function(data) {
      try {
        return JSONBig.parse(data) // 将后台返回的数据转换为 {id: BigNumber, name: "张三"} 类型的对象
      } catch (error) {
        return data
      }
    }
  ]
})
// 请求拦截器
// Add a request interceptor
request.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    const { user } = store.state
    if (user && user.token) {
      config.headers.Authorization = `Bearer ${user.token}`
    }
    // console.log(config.headers.Authorization)
    return config
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error)
  }
)
// 响应拦截器
export default request
